package com.example.yx.demo20;


import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")//读取配置文件
@EnableConfigurationProperties({WebMvcProperties.class, ServerProperties.class})//读取配置文件中以spring.mvc开头的配置以及以server开头的配置
public class WebConfig {

    //内嵌 web 容器工厂
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(ServerProperties serverProperties) {
        Integer port = serverProperties.getPort();
        return new TomcatServletWebServerFactory(port);
    }
    //创建 DispatcherServlet
    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet();
    }
    //注册 DispatcherServlet，Spring MVC 入口
    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(DispatcherServlet dispatcherServlet
            ,WebMvcProperties webMvcProperties) {
        DispatcherServletRegistrationBean registrationBean = new DispatcherServletRegistrationBean(dispatcherServlet, "/");
        int load = webMvcProperties.getServlet().getLoadOnStartup();
        //这个方法表明tomcat一启动就对这个servlet进行初始化
        registrationBean.setLoadOnStartup(load);//表示优先级，如果到时候有多个servlet，会按照这些优先级进行初始化的排序
        return registrationBean;
    }

    //如果用 DispatcherServlet 来进行默认的初始化那些组件，它们就不回作为Bean被添加到容器中，因此，我们手动将他们注册进去
    //1. 加入RequestMappingHandlerMapping
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    //2.加入RequestMappingHandlerAdapter
    @Bean
    public MyRequestMappingHandlerAdapter requestMappingHandlerAdapter () {
        TokenArgumentResolver resolver = new TokenArgumentResolver();
        MyRequestMappingHandlerAdapter adapter = new MyRequestMappingHandlerAdapter();
        YmlReturnValueHandler handler = new YmlReturnValueHandler();
        List<HandlerMethodArgumentResolver> list = new ArrayList<>();
        List<HandlerMethodReturnValueHandler> list1 = new ArrayList<>();

        list.add(resolver);
        list1.add(handler);
        adapter.setArgumentResolvers(list);
        adapter.setCustomReturnValueHandlers(list1);
        return adapter;
    }


}
